﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorchView.SharedStashEditor.Helpers;
using TorchView.Game;
using TorchView.Windows;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using System.Windows;
using System.Collections.Specialized;
using System.ComponentModel;

namespace TorchView.SharedStashEditor.ViewModels
{
    public class EffectListViewModel : INotifyPropertyChanged
    {
        private IUnityContainer _Container;
        private ItemViewModel _itemVM;
        private EffectActivationType _EffectType;
        private bool _modified;

        public EffectListViewModel(EffectActivationType EffectType, ItemViewModel itemVM, Effect[] effects)
        {
            _Container = SharedStashEditorPlugin.Container;
            _EffectType = EffectType;
            _itemVM = itemVM;

            EffectList = new ObservableCollection2<Effect>();
            foreach (Effect effect in effects)
            {
                EffectList.Add(new Effect(effect));
            }

            EffectList.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);

            _AddEffectCommand = new DirectCommand(ExecuteAddEffectCommand);
            _EditEffectCommand = new DirectCommand(ExecuteEditEffectCommand);
            _RemoveEffectCommand = new DirectCommand(ExecuteRemoveEffectCommand);
        }

        private void CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
        {
            Modified = true;
            NotifyPropertyChanged("Name");
        }

        private void ExecuteAddEffectCommand(object parameter)
        {
            var viewModel = _Container.Resolve<IAddEffectViewModel>(new PropertyOverride("Owner", Application.Current.MainWindow));
            if (viewModel.View.ShowDialog() ?? false)
            {
                if (viewModel.SelectedEffect != null)
                {
                    Effect gameEffect = viewModel.SelectedEffect.CreateGameEffect();
                    gameEffect.EffectType = _EffectType;
                    EffectList.Add(gameEffect);
                }
            }
        }

        private void ExecuteEditEffectCommand(object parameter)
        {
            if (SelectedEffect != null)
            {
                var viewModel = _Container.Resolve<IEditEffectViewModel>(new PropertyOverride("Owner", Application.Current.MainWindow));
                viewModel.effect = SelectedEffect;
                if (viewModel.View.ShowDialog() ?? false)
                {
                    EffectList.Refresh();
                    Modified = true;
                    NotifyPropertyChanged("Name");
                }
            }
        }

        private void ExecuteRemoveEffectCommand(object parameter)
        {
            if (SelectedEffect != null)
            {
                if (_Container.Resolve<IMessageBoxService>().Show(Application.Current.MainWindow, string.Format("Are you sure you want to remove effect [{0}]?", SelectedEffect), "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes)
                {
                    EffectList.Remove(SelectedEffect);
                }
            }
        }

        public void Add(Effect effect)
        {
            EffectList.Add(effect);
        }

        public bool Modified 
        {
            get { return _modified; }
            set
            {
                if (value)
                {
                    _itemVM.Modified = true;
                }
                _modified = value;
            }
        }

        public string Name
        {
            get
            {
                return _EffectType + " Effect (" + EffectList.Count + ")";
            }
        }
        public ObservableCollection2<Effect> EffectList { get; set; }
        public Effect SelectedEffect { get; set; }

        private DirectCommand _AddEffectCommand;
        public ICommand AddEffectCommand { get { return _AddEffectCommand; } }

        private DirectCommand _EditEffectCommand;
        public ICommand EditEffectCommand { get { return _EditEffectCommand; } }

        private DirectCommand _RemoveEffectCommand;
        public ICommand RemoveEffectCommand { get { return _RemoveEffectCommand; } }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
